1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
mod ops;

use crate::{
    bindings::*,
    std_types::{c_float, cosf, floorf, sinf, sqrtf},
    Vec3_IsZero, Vec3_Set,
};

impl Vec3 {
    pub const fn new(x: c_float, y: c_float, z: c_float) -> Self {
        Self { x, y, z }
    }

    pub const fn zero() -> Self {
        Self {
            x: 0.0,
            y: 0.0,
            z: 0.0,
        }
    }

    pub const fn big_pos() -> Self {
        Vec3_BigPos()
    }

    pub const fn create(x: c_float, y: c_float, z: c_float) -> Self {
        Vec3_Create3(x, y, z)
    }

    pub fn set(&mut self, x: c_float, y: c_float, z: c_float) {
        Vec3_Set!(self, x, y, z);
    }

    pub fn is_zero(&self) -> bool {
        Vec3_IsZero!(self)
    }

    pub fn length_squared(&self) -> c_float {
        Vec3_LengthSquared(self)
    }

    #[must_use]
    pub fn lerp(&self, b: Vec3, blend: c_float) -> Self {
        let mut result = Self::zero();
        Vec3_Lerp(&mut result, self, &b, blend);
        result
    }

    #[must_use]
    pub fn normalize(&self) -> Self {
        let mut result = Self::zero();
        Vec3_Normalize(&mut result, self);
        result
    }

    #[must_use]
    pub fn transform(&self, mat: Matrix) -> Self {
        let mut result = Self::zero();
        Vec3_Transform(&mut result, self, &mat);
        result
    }

    pub fn transform_y(y: c_float, mat: Matrix) -> Self {
        let mut result = Self::zero();
        Vec3_TransformY(&mut result, y, &mat);
        result
    }

    pub fn rotate_x(v: Vec3, angle: c_float) -> Self {
        Vec3_RotateX(v, angle)
    }

    pub fn rotate_y(v: Vec3, angle: c_float) -> Self {
        Vec3_RotateY(v, angle)
    }

    pub fn rotate_y3(x: c_float, y: c_float, z: c_float, angle: c_float) -> Self {
        Vec3_RotateY3(x, y, z, angle)
    }

    pub fn rotate_z(v: Vec3, angle: c_float) -> Self {
        Vec3_RotateZ(v, angle)
    }

    pub fn floor(&self) -> IVec3 {
        let mut result = IVec3::zero();
        IVec3_Floor(&mut result, self);
        result
    }

    pub fn get_dir_vector(yawRad: c_float, pitchRad: c_float) -> Self {
        Vec3_GetDirVector(yawRad, pitchRad)
    }
}

pub const fn Vec3_BigPos() -> Vec3 {
    Vec3 {
        x: 1e25_f32,
        y: 1e25_f32,
        z: 1e25_f32,
    }
}

pub const fn Vec3_Create3(x: c_float, y: c_float, z: c_float) -> Vec3 {
    Vec3 { x, y, z }
}

/// Returns the squared length of the vector.
/// Squared length can be used for comparison, to avoid a costly sqrt()
/// However, you must sqrt() this when adding lengths.
pub fn Vec3_LengthSquared(v: &Vec3) -> c_float {
    v.x * v.x + v.y * v.y + v.z * v.z
}

/// Linearly interpolates components of two vectors.
pub fn Vec3_Lerp(result: &mut Vec3, a: &Vec3, b: &Vec3, blend: c_float) {
    result.x = blend * (b.x - a.x) + a.x;
    result.y = blend * (b.y - a.y) + a.y;
    result.z = blend * (b.z - a.z) + a.z;
}

/// Scales all components of a vector to lie in [-1, 1]
pub fn Vec3_Normalize(result: &mut Vec3, a: &Vec3) {
    let lenSquared = a.x * a.x + a.y * a.y + a.z * a.z;
    let scale = 1.0 / sqrtf(lenSquared);
    result.x = a.x * scale;
    result.y = a.y * scale;
    result.z = a.z * scale;
}

/// Transforms a vector by the given matrix.
pub fn Vec3_Transform(result: &mut Vec3, a: &Vec3, mat: &Matrix) {
    // a could be pointing to result - can't directly assign x/y/z therefore
    let x = a.x * mat.row1.x + a.y * mat.row2.x + a.z * mat.row3.x + mat.row4.x;
    let y = a.x * mat.row1.y + a.y * mat.row2.y + a.z * mat.row3.y + mat.row4.y;
    let z = a.x * mat.row1.z + a.y * mat.row2.z + a.z * mat.row3.z + mat.row4.z;
    result.x = x;
    result.y = y;
    result.z = z;
}

/// Same as Vec3_Transform, but faster since x and z are assumed as 0.
pub fn Vec3_TransformY(result: &mut Vec3, y: c_float, mat: &Matrix) {
    result.x = y * mat.row2.x + mat.row4.x;
    result.y = y * mat.row2.y + mat.row4.y;
    result.z = y * mat.row2.z + mat.row4.z;
}

pub fn Vec3_RotateX(v: Vec3, angle: c_float) -> Vec3 {
    let cosA = cosf(angle);
    let sinA = sinf(angle);
    Vec3_Create3(v.x, cosA * v.y + sinA * v.z, -sinA * v.y + cosA * v.z)
}

pub fn Vec3_RotateY(v: Vec3, angle: c_float) -> Vec3 {
    let cosA = cosf(angle);
    let sinA = sinf(angle);
    Vec3_Create3(cosA * v.x - sinA * v.z, v.y, sinA * v.x + cosA * v.z)
}

pub fn Vec3_RotateY3(x: c_float, y: c_float, z: c_float, angle: c_float) -> Vec3 {
    let cosA = cosf(angle);
    let sinA = sinf(angle);
    Vec3_Create3(cosA * x - sinA * z, y, sinA * x + cosA * z)
}

pub fn Vec3_RotateZ(v: Vec3, angle: c_float) -> Vec3 {
    let cosA = cosf(angle);
    let sinA = sinf(angle);
    Vec3_Create3(cosA * v.x + sinA * v.y, -sinA * v.x + cosA * v.y, v.z)
}

/// Whether all of the components of the two vectors are equal.
pub fn Vec3_Equals(a: &Vec3, b: &Vec3) -> bool {
    #[allow(clippy::float_cmp)]
    {
        a.x == b.x && a.y == b.y && a.z == b.z
    }
}

pub fn IVec3_Floor(result: &mut IVec3, a: &Vec3) {
    result.x = floorf(a.x) as _;
    result.y = floorf(a.y) as _;
    result.z = floorf(a.z) as _;
}

/// Returns a normalised vector facing in the direction described by the given yaw and pitch.
pub fn Vec3_GetDirVector(yawRad: c_float, pitchRad: c_float) -> Vec3 {
    let x = -cosf(pitchRad) * -sinf(yawRad);
    let y = -sinf(pitchRad);
    let z = -cosf(pitchRad) * cosf(yawRad);
    Vec3_Create3(x, y, z)
}